
import { getUserInfoAPI, loginAPI } from '@/api'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  state: {
    token: getToken(), // 本地获取数据
    userInfo: {}
  },
  mutations: {
    // 本地储存数据
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    // 删除token
    removeToken(state) {
      state.token = null
      removeToken() // 删除本地缓存的token
    },
    // 定义更新数据用户信息
    setUserInfo(state, paload) {
      state.userInfo = paload
    }
  },
  actions: {
    async login(store, data) {
      const token = await loginAPI(data)
      store.commit('setToken', token)
      // store.commit('setToken', await loginAPI(data))
    },
    // 设置用户信息
    async getUserInfo(store) {
      store.commit('setUserInfo', await getUserInfoAPI())
    },
    // 登出
    logout(store) {
      store.commit('removeToken')
      store.commit('setUserInfo', {})
    }
  },
  namespaced: true
}
